package lhs.controller;

import lhs.pojo.User;
import lhs.service.UserService;
import lhs.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;
        //显示输入页面
        @RequestMapping(value = "/showSaveUser")
        public String showUser(){
            return "saveUser";
        }
        //保存信息到数据表     //新增
        @RequestMapping(value = "/addUser")
        public String saveUser(User user){
            userService.saveUser(user);
            return "redirect:showAllUser";
        }
//        //删除
        @RequestMapping(value = "/delUser")
        public String delUser(int bh){
            int a=userService.delUser(bh);
            return "redirect:showAllUser";
        }
        //数据库记录显示
    @RequestMapping(value = "/showAllUser")
        public ModelAndView showAllUser(User user,String pageNow){
        ModelAndView mv=new ModelAndView();
        try {
            //获取当前总行数
            int totalcount = userService.userCount();
            //当前页面pageNow，从jsp页面获取
            Page page = null;
            if (pageNow != null) {
                page = new Page(totalcount, Integer.parseInt(pageNow));
            } else {
                page = new Page(totalcount, 1);
            }
            //User user=new User();
            user.setStart(page.getStartPos());
            user.setPageSize(page.getPageSize());
            List<User> list = userService.showAllUser(user);
            mv.addObject("list", list);
            mv.addObject("page",page);
            mv.setViewName("showAllUser");

        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
        }
    //根据编号获取实体并修改
    @RequestMapping("/editUser")
    public ModelAndView editUser(int bh){
            ModelAndView mv=new ModelAndView();
            User user=userService.getUserByBh(bh);
            mv.addObject("user",user);
            mv.setViewName("editUser");
            return mv;
    }
    //修改实体
    @RequestMapping("/updateUser")
    public String updateUser(User user){
            int num=userService.updateUser(user);
            return  "redirect:showAllUser";
    }
    //详情页面
    @RequestMapping("/tipUser")
    public ModelAndView tipUser(int bh){
        ModelAndView mv=new ModelAndView();
        User user=userService.getUserByBh(bh);
        mv.setViewName("tipUser");
        mv.addObject("user",user);
        return mv;
    }
    //分页查询
    @RequestMapping("/listUserPage")
    public ModelAndView listUserPage(String pageNow){
        ModelAndView mv=new ModelAndView();
        HashMap map=new HashMap();
        try{
            //获取当前总行数
            int totalcount=userService.userCount();
            //当前页面pageNow，从jsp页面获取
            Page page=null;
            if (pageNow!=null){
                page=new Page(totalcount,Integer.parseInt(pageNow));
            }else {
                page=new Page(totalcount,1);
            }
            map.put("start",page.getStartPos());//定义开始位置
            map.put("pageSize",page.getPageSize());//定义每页显示行数

            List<User> list=userService.listUserPage(map);
            mv.setViewName("listUserPage");
            mv.addObject("list",list);
            mv.addObject("page",page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }


        public UserService getUserService() {
        return userService;
    }

        public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
